
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Java,Spring,Junit,Jacoco," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>spring-junit-jacoco代码覆盖测试 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        spring-junit-jacoco代码覆盖测试
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Jacoco/" rel="tag">Jacoco</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Java/" rel="tag">Java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Junit/" rel="tag">Junit</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Spring/" rel="tag">Spring</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 14 分钟</span>
    </header>

    <div class="post-content">
      <p>接着上一篇<a href="http://xcoding.tech/2018/12/06/junit-jacoco/junit-jacoco%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E6%B5%8B%E8%AF%95/">http://xcoding.tech/2018/12/06/junit-jacoco/junit-jacoco%E4%BB%A3%E7%A0%81%E8%A6%86%E7%9B%96%E6%B5%8B%E8%AF%95/</a>。</p>
<p>上一篇只是简单的java项目，实际应用通常会复杂很多，比如spring项目，那么本篇介绍如何在spring项目中做代码覆盖率扫描并生成报告。</p>
<h3 id="使用maven构建spring项目"><a href="#使用maven构建spring项目" class="headerlink" title="使用maven构建spring项目"></a>使用maven构建spring项目</h3><h4 id="清空原有目录"><a href="#清空原有目录" class="headerlink" title="清空原有目录"></a>清空原有目录</h4><p>复制上一篇的项目，命名为<code>coverage-spring</code>，然后把目录下面<code>src</code>和<code>test</code>目录都删除了，里面的代码也不需要保留。删除之后项目目录结构如下：</p>
<p><img src="/image/blog-image/java/junit-coverage/1.png" alt="项目结构"></p>
<p>另外，再把<code>jacocolib</code>目录下的<code>hamcrest-core-1.3.jar</code>、<code>junit-4.12.jar</code>也删除了，一会使用maven重新下载。</p>
<h4 id="设置maven项目结构"><a href="#设置maven项目结构" class="headerlink" title="设置maven项目结构"></a>设置maven项目结构</h4><p>修改项目结构，在项目属性的<code>java build path</code>里面设置，设置后结构如下截图：</p>
<p><img src="/image/blog-image/java/junit-coverage/2.png" alt="项目结构"></p>
<p><strong>注意</strong>：对于没有的目录要自己创建（如截图左下角的<code>create new folder</code>），一级一级目录创建，创建完毕之后按截图勾选。</p>
<p>设置后目录结构如下：</p>
<p><img src="/image/blog-image/java/junit-coverage/3.png" alt="项目结构"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;建立工程目录</span><br><span class="line">src&#x2F;main&#x2F;java&#x2F;com&#x2F;ubuntuvim&#x2F;coverage</span><br><span class="line">&#x2F;&#x2F;建立test目录</span><br><span class="line">src&#x2F;test&#x2F;java&#x2F;com&#x2F;ubuntuvim&#x2F;coverage</span><br><span class="line">&#x2F;&#x2F;建立resources文件夹</span><br><span class="line">src&#x2F;main&#x2F;resources</span><br><span class="line">&#x2F;&#x2F; 创建应用主配置</span><br><span class="line">src&#x2F;main&#x2F;resources&#x2F;application.xml</span><br><span class="line">&#x2F;&#x2F;建立webapp目录</span><br><span class="line">src&#x2F;main&#x2F;webapp&#x2F;</span><br></pre></td></tr></table></figure>

<p>文件具体内容本文后续逐个补充，创建的时候暂时为空文件。</p>
<h4 id="增加pom-xml"><a href="#增加pom-xml" class="headerlink" title="增加pom.xml"></a>增加pom.xml</h4><p>项目结构创建完毕之后，在根目录下新增<code>pom.xml</code>，文件内容如下：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.ubuntuvim.coverage<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>coverage-spring<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">packaging</span>&gt;</span>jar<span class="tag">&lt;/<span class="name">packaging</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.0.1-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">name</span>&gt;</span>spring proj coverage test<span class="tag">&lt;/<span class="name">name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">url</span>&gt;</span>http://maven.apache.org<span class="tag">&lt;/<span class="name">url</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 属性配置 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">project.build.sourceEncoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">project.build.sourceEncoding</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">spring.version</span>&gt;</span>5.1.2.RELEASE<span class="tag">&lt;/<span class="name">spring.version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">project.master.classpath.dir</span>&gt;</span>master-lib<span class="tag">&lt;/<span class="name">project.master.classpath.dir</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-webmvc<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;spring.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;spring.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-context<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;spring.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-beans<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;spring.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-aop<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;spring.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;spring.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-expression<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;spring.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- https://mvnrepository.com/artifact/junit/junit --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.12<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>javax.servlet<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>servlet-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.5<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">scope</span>&gt;</span>provided<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">build</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">plugins</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">plugin</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.maven.plugins<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>maven-dependency-plugin<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">executions</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">execution</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">id</span>&gt;</span>copy<span class="tag">&lt;/<span class="name">id</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">phase</span>&gt;</span>package<span class="tag">&lt;/<span class="name">phase</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">goals</span>&gt;</span></span><br><span class="line">                            <span class="tag">&lt;<span class="name">goal</span>&gt;</span>copy-dependencies<span class="tag">&lt;/<span class="name">goal</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;/<span class="name">goals</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">                            <span class="tag">&lt;<span class="name">outputDirectory</span>&gt;</span></span><br><span class="line">                                $&#123;project.master.classpath.dir&#125;</span><br><span class="line">                            <span class="tag">&lt;/<span class="name">outputDirectory</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;/<span class="name">execution</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">executions</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">plugin</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">plugins</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">build</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="安装maven依赖"><a href="#安装maven依赖" class="headerlink" title="安装maven依赖"></a>安装maven依赖</h4><p>创建好<code>pom.xml</code>之后，在这个文件上右键 <code>Run As</code> -&gt; <code>maven install</code>。稍等片刻spring依赖自动下载到根目录下的<code>master-lib</code>目录中。把jar都下载都根目录的<code>master-lib</code>中是为了后续编译代码时候引用方便，通过<code>pom.xml</code>中的<code>outputDirectory</code>标签设置了jar的保存目录。</p>
<h3 id="增加业务代码"><a href="#增加业务代码" class="headerlink" title="增加业务代码"></a>增加业务代码</h3><p>所需jar包下载到<code>master-lib</code>之后就可以编写spring代码了。在此之前先把下载的jar全部添加到项目类路径。</p>
<h4 id="spring配置"><a href="#spring配置" class="headerlink" title="spring配置"></a>spring配置</h4><p>打开<code>src/main/resources/application.xml</code>增加如下内容：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:jee</span>=<span class="string">&quot;http://www.springframework.org/schema/jee&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:context</span>=<span class="string">&quot;http://www.springframework.org/schema/context&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:p</span>=<span class="string">&quot;http://www.springframework.org/schema/p&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:mvc</span>=<span class="string">&quot;http://www.springframework.org/schema/mvc&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xmlns:util</span>=<span class="string">&quot;http://www.springframework.org/schema/util&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans </span></span></span><br><span class="line"><span class="tag"><span class="string">           http://www.springframework.org/schema/beans/spring-beans-4.1.xsd </span></span></span><br><span class="line"><span class="tag"><span class="string">             http://www.springframework.org/schema/context </span></span></span><br><span class="line"><span class="tag"><span class="string">             http://www.springframework.org/schema/context/spring-context-4.0.xsd </span></span></span><br><span class="line"><span class="tag"><span class="string">             http://www.springframework.org/schema/jee </span></span></span><br><span class="line"><span class="tag"><span class="string">              http://www.springframework.org/schema/jee/spring-jee-4.1.xsd </span></span></span><br><span class="line"><span class="tag"><span class="string">              http://www.springframework.org/schema/mvc </span></span></span><br><span class="line"><span class="tag"><span class="string">             http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd </span></span></span><br><span class="line"><span class="tag"><span class="string">             http://www.springframework.org/schema/util </span></span></span><br><span class="line"><span class="tag"><span class="string">             http://www.springframework.org/schema/util/spring-util-4.1.xsd&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:annotation-config</span> /&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 设置自动扫描的package --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.ubuntuvim.coverage&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mvc:annotation-driven</span> /&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span> </span><br></pre></td></tr></table></figure>

<h4 id="验证项目"><a href="#验证项目" class="headerlink" title="验证项目"></a>验证项目</h4><p>配置都准备好之后先验证一下项目是否搭建成功了。创建一个service类，然后用Junit启动这个类，验证spring容器是否配置成功了。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ubuntuvim.coverage.service.impl;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.ubuntuvim.coverage.domain.User;</span><br><span class="line"><span class="keyword">import</span> com.ubuntuvim.coverage.service.IUserService;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceImpl</span> <span class="keyword">implements</span> <span class="title">IUserService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> User <span class="title">getUser</span><span class="params">(String id)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//为了简单直接创建一个用户对象，通常这部分是通过查询数据库获取</span></span><br><span class="line">        User u = <span class="keyword">new</span> User();</span><br><span class="line">        u.setAge(<span class="number">20</span>);</span><br><span class="line">        u.setId(<span class="string">&quot;123&quot;</span>);</span><br><span class="line">        u.setUsername(<span class="string">&quot;ubuntuvim&quot;</span>);</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> u;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Spring-Test配置"><a href="#Spring-Test配置" class="headerlink" title="Spring Test配置"></a>Spring Test配置</h3><p>业务类创建完毕之后我们新建一个Junit类用于测试这个service类。在创建测试类之前先增加一些其他代码，用于加载配置，设置web环境等，后续所有的测试都需要继承这个基类。</p>
<h4 id="创建BaseJunit4Test基类"><a href="#创建BaseJunit4Test基类" class="headerlink" title="创建BaseJunit4Test基类"></a>创建BaseJunit4Test基类</h4><p>该类主要用来加载配置文件，设置web环境。所有的测试类，都继承该类即可。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ubuntuvim.coverage.base;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.runner.RunWith;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.ContextConfiguration;  </span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.junit4.SpringJUnit4ClassRunner;</span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.web.WebAppConfiguration;  </span><br><span class="line">  </span><br><span class="line"><span class="meta">@RunWith(SpringJUnit4ClassRunner.class)</span> <span class="comment">//使用junit4进行测试  </span></span><br><span class="line"><span class="comment">// 加载spring主配置文件</span></span><br><span class="line"><span class="meta">@ContextConfiguration(locations=&#123;&quot;classpath:application.xml&quot;&#125;)</span> </span><br><span class="line"><span class="comment">// 设置web资料目录</span></span><br><span class="line"><span class="meta">@WebAppConfiguration(&quot;src/main/webapp&quot;)</span></span><br><span class="line"><span class="comment">//------------如果加入以下代码，所有继承该类的测试类都会遵循该配置，也可以不加，在测试类的方法上</span></span><br><span class="line"><span class="comment">///控制事务，参见下一个实例    </span></span><br><span class="line"><span class="comment">//这个非常关键，如果不加入这个注解配置，事务控制就会完全失效！    </span></span><br><span class="line"><span class="comment">//@Transactional    </span></span><br><span class="line"><span class="comment">//这里的事务关联到配置文件中的事务控制器（transactionManager = &quot;transactionManager&quot;），同时</span></span><br><span class="line"><span class="comment">//指定自动回滚（defaultRollback = true）。这样做操作的数据才不会污染数据库！    </span></span><br><span class="line"><span class="comment">//@TransactionConfiguration(transactionManager = &quot;transactionManager&quot;, defaultRollback = true)    </span></span><br><span class="line"><span class="comment">//------------</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AbstractBaseJunit4Test</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>业务代码测试类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ubuntuvim.coverage.service.impl;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.junit.Assert.assertNotNull;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.ubuntuvim.coverage.base.AbstractBaseJunit4Test;</span><br><span class="line"><span class="keyword">import</span> com.ubuntuvim.coverage.domain.User;</span><br><span class="line"><span class="keyword">import</span> com.ubuntuvim.coverage.service.IUserService;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceImplTest</span> <span class="keyword">extends</span> <span class="title">AbstractBaseJunit4Test</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> IUserService iUserService;</span><br><span class="line">    </span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testGetUser</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        User u = iUserService.getUser(<span class="string">&quot;123&quot;</span>);</span><br><span class="line">        assertNotNull(u);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>右键 -&gt; <code>Coverage As</code> -&gt; <code>Junit Test</code>。运行完毕之后可以看到覆盖率结果：</p>
<p><img src="/image/blog-image/java/junit-coverage/4.png" alt="覆盖率结果"></p>
<h3 id="整合Ant自动扫描"><a href="#整合Ant自动扫描" class="headerlink" title="整合Ant自动扫描"></a>整合Ant自动扫描</h3><p>到此为止，项目环境是验证通过了，直接使用覆盖率运行也是没问题的。那么下一步就是整合Ant，使用Ant脚本自动运行测试类并生成覆盖率报告。</p>
<p>复制第一篇创建的项目中的<code>build.xml</code>和<code>build-test.xml</code>。重命名为<code>compile.xml</code>和<code>coverage-report.xml</code>，你也可以不重命名，我只是为了用一个更直观的名字所以改一改文件名。</p>
<p>稍微调整一下<code>buil.xml</code>的内容，如下：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">name</span>=<span class="string">&quot;AntTestReporting&quot;</span> <span class="attr">basedir</span>=<span class="string">&quot;.&quot;</span> <span class="attr">default</span>=<span class="string">&quot;compile&quot;</span>&gt;</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">&lt;!-- general --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;DEBUG&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;VERBOSE&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;TARGET&quot;</span> <span class="attr">value</span>=<span class="string">&quot;1.8&quot;</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">&lt;!-- folder --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;build.dir&quot;</span> <span class="attr">value</span>=<span class="string">&quot;build&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;src.dir&quot;</span> <span class="attr">value</span>=<span class="string">&quot;src/main/java&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;src.classess.dir&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;build.dir&#125;/classess&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;test.dir&quot;</span> <span class="attr">value</span>=<span class="string">&quot;src/test/java&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;test.classess.dir&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;build.dir&#125;/test-classess&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;master-lib&quot;</span> <span class="attr">value</span>=<span class="string">&quot;master-lib&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;third-party-lib&quot;</span> <span class="attr">value</span>=<span class="string">&quot;jacocolib&quot;</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">&lt;!-- 设置编译所需要的所有jar，包括从maven下载的和覆盖率扫描的第三方jar --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">path</span> <span class="attr">id</span>=<span class="string">&quot;classpath&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">fileset</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;master-lib&#125;&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">include</span> <span class="attr">name</span>=<span class="string">&quot;**/*.jar&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">fileset</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">fileset</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;third-party-lib&#125;&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">include</span> <span class="attr">name</span>=<span class="string">&quot;**/*.jar&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">fileset</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">path</span>&gt;</span></span><br><span class="line"> </span><br><span class="line">    <span class="tag">&lt;<span class="name">target</span> <span class="attr">name</span>=<span class="string">&quot;init&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">delete</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;build.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">            </span><br><span class="line">        <span class="tag">&lt;<span class="name">mkdir</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;build.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mkdir</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;src.classess.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mkdir</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;test.classess.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">target</span>&gt;</span></span><br><span class="line"> </span><br><span class="line">    <span class="tag">&lt;<span class="name">target</span> <span class="attr">name</span>=<span class="string">&quot;compile&quot;</span> <span class="attr">depends</span>=<span class="string">&quot;init&quot;</span>&gt;</span></span><br><span class="line"> </span><br><span class="line">         <span class="comment">&lt;!-- 编译java代码 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">javac</span> <span class="attr">target</span>=<span class="string">&quot;$&#123;TARGET&#125;&quot;</span> <span class="attr">debug</span>=<span class="string">&quot;$&#123;DEBUG&#125;&quot;</span> <span class="attr">verbose</span>=<span class="string">&quot;$&#123;VERBOSE&#125;&quot;</span> <span class="attr">classpathref</span>=<span class="string">&quot;classpath&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">optimize</span>=<span class="string">&quot;true&quot;</span> <span class="attr">destdir</span>=<span class="string">&quot;$&#123;src.classess.dir&#125;&quot;</span> <span class="attr">srcdir</span>=<span class="string">&quot;$&#123;src.dir&#125;&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">javac</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">echo</span>&gt;</span>======== java代码编译完毕 ========<span class="tag">&lt;/<span class="name">echo</span>&gt;</span></span><br><span class="line">        </span><br><span class="line">        <span class="tag">&lt;<span class="name">path</span> <span class="attr">id</span>=<span class="string">&quot;classpath.src.dir&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">pathelement</span> <span class="attr">location</span>=<span class="string">&quot;$&#123;src.classess.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">path</span>&gt;</span></span><br><span class="line">         <span class="comment">&lt;!-- 编译test代码 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">javac</span> <span class="attr">target</span>=<span class="string">&quot;$&#123;TARGET&#125;&quot;</span> <span class="attr">debug</span>=<span class="string">&quot;$&#123;DEBUG&#125;&quot;</span> <span class="attr">verbose</span>=<span class="string">&quot;$&#123;VERBOSE&#125;&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">optimize</span>=<span class="string">&quot;true&quot;</span> <span class="attr">destdir</span>=<span class="string">&quot;$&#123;test.classess.dir&#125;&quot;</span> <span class="attr">srcdir</span>=<span class="string">&quot;$&#123;test.dir&#125;&quot;</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- test代码依赖src的代码和第三方jar --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">classpath</span> <span class="attr">refid</span>=<span class="string">&quot;classpath&quot;</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">classpath</span> <span class="attr">refid</span>=<span class="string">&quot;classpath.src.dir&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">javac</span>&gt;</span></span><br><span class="line">         <span class="tag">&lt;<span class="name">echo</span>&gt;</span>======== test代码编译完毕 ========<span class="tag">&lt;/<span class="name">echo</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">target</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>运行ant脚本之后，刷新项目可以看到多了一个<code>build</code>目录，在目录下可以看到所有编译后的class文件。</p>
<p><img src="/image/blog-image/java/junit-coverage/5.png" alt="编译结果"></p>
<p>编译完成之后再使用Ant执行测试类代码，脚本仍然是在前一个项目基础上改造而得。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">name</span>=<span class="string">&quot;AntTestReporting-test&quot;</span> <span class="attr">basedir</span>=<span class="string">&quot;.&quot;</span> <span class="attr">default</span>=<span class="string">&quot;main&quot;</span> <span class="attr">xmlns:jacoco</span>=<span class="string">&quot;antlib:org.jacoco.ant&quot;</span>&gt;</span></span><br><span class="line"> </span><br><span class="line">    <span class="tag">&lt;<span class="name">import</span> <span class="attr">file</span>=<span class="string">&quot;compile.xml&quot;</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line">    <span class="comment">&lt;!-- Java Code Coverage --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">taskdef</span> <span class="attr">uri</span>=<span class="string">&quot;antlib:org.jacoco.ant&quot;</span> <span class="attr">resource</span>=<span class="string">&quot;org/jacoco/ant/antlib.xml&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">classpath</span> <span class="attr">path</span>=<span class="string">&quot;jacocolib/jacocoant.jar&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">taskdef</span>&gt;</span></span><br><span class="line">     </span><br><span class="line">    <span class="comment">&lt;!-- spring配置 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;spring.cfg.dir&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;basedir&#125;/src/main/resources&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;reports.dir&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;basedir&#125;/reports&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;test.data.dir&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;reports.dir&#125;/testResults&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;coverage.reports.dir&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;reports.dir&#125;/coverage-report&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;junit.reports.dir&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;reports.dir&#125;/junit-report&quot;</span> /&gt;</span></span><br><span class="line"> </span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;xms&quot;</span> <span class="attr">value</span>=<span class="string">&quot;-Xms256m&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;xmx&quot;</span> <span class="attr">value</span>=<span class="string">&quot;-Xmx1024m&quot;</span> /&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">&lt;!-- test代码类路径，包括src代码、测试代码本身、jacoco jar --&gt;</span>    </span><br><span class="line">     <span class="tag">&lt;<span class="name">path</span> <span class="attr">id</span>=<span class="string">&quot;classpath.test.dir&quot;</span>&gt;</span></span><br><span class="line">         <span class="comment">&lt;!-- 测试代码依赖src代码，需要作为test代码的运行classpath --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">path</span> <span class="attr">refid</span>=<span class="string">&quot;classpath&quot;</span>/&gt;</span></span><br><span class="line">         <span class="tag">&lt;<span class="name">pathelement</span> <span class="attr">location</span>=<span class="string">&quot;$&#123;src.classess.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">pathelement</span> <span class="attr">location</span>=<span class="string">&quot;$&#123;test.classess.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">path</span>&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="tag">&lt;<span class="name">target</span> <span class="attr">name</span>=<span class="string">&quot;init-report-dir&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">delete</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;test.data.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">delete</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;coverage.reports.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">delete</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;reports.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">        </span><br><span class="line">        <span class="tag">&lt;<span class="name">mkdir</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;reports.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mkdir</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;test.data.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mkdir</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;junit.reports.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mkdir</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;coverage.reports.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">target</span>&gt;</span></span><br><span class="line">            </span><br><span class="line">       <span class="tag">&lt;<span class="name">target</span> <span class="attr">name</span>=<span class="string">&quot;copy-spring-cfg&quot;</span> <span class="attr">description</span>=<span class="string">&quot;复制spring配置文件到test运行目录&quot;</span>&gt;</span></span><br><span class="line">           <span class="tag">&lt;<span class="name">echo</span>&gt;</span>复制spring配置到test运行目录<span class="tag">&lt;/<span class="name">echo</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">copy</span> <span class="attr">todir</span>=<span class="string">&quot;$&#123;test.classess.dir&#125;&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">fileset</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;spring.cfg.dir&#125;&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">include</span> <span class="attr">name</span>=<span class="string">&quot;**/*.xml&quot;</span> /&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">include</span> <span class="attr">name</span>=<span class="string">&quot;**/*.properties&quot;</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">fileset</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">copy</span>&gt;</span>    </span><br><span class="line">    <span class="tag">&lt;/<span class="name">target</span>&gt;</span></span><br><span class="line">        </span><br><span class="line">    <span class="tag">&lt;<span class="name">target</span> <span class="attr">name</span>=<span class="string">&quot;run-test&quot;</span> <span class="attr">depends</span>=<span class="string">&quot;init-report-dir, compile, copy-spring-cfg&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">echo</span>&gt;</span>========= 运行所有test类 =========<span class="tag">&lt;/<span class="name">echo</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">jacoco:coverage</span> <span class="attr">destfile</span>=<span class="string">&quot;$&#123;test.data.dir&#125;/jacoco.exec&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">junit</span> <span class="attr">printsummary</span>=<span class="string">&quot;true&quot;</span> <span class="attr">haltonfailure</span>=<span class="string">&quot;false&quot;</span> <span class="attr">fork</span>=<span class="string">&quot;yes&quot;</span> <span class="attr">forkmode</span>=<span class="string">&quot;once&quot;</span> <span class="attr">showoutput</span>=<span class="string">&quot;yes&gt;</span></span></span><br><span class="line"><span class="tag"><span class="string">                &lt;jvmarg value=&quot;</span>$&#123;<span class="attr">xms</span>&#125;&quot; /&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">jvmarg</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;xmx&#125;&quot;</span> /&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">classpath</span> <span class="attr">refid</span>=<span class="string">&quot;classpath.test.dir&quot;</span> /&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">formatter</span> <span class="attr">type</span>=<span class="string">&quot;xml&quot;</span> /&gt;</span></span><br><span class="line">                <span class="comment">&lt;!-- 执行 *Test类 --&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">batchtest</span> <span class="attr">todir</span>=<span class="string">&quot;$&#123;test.data.dir&#125;&quot;</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">fileset</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;test.classess.dir&#125;&quot;</span>&gt;</span></span><br><span class="line">                        <span class="comment">&lt;!-- AbstractBaseJunit4Test类不需要执行 --&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">exclude</span> <span class="attr">name</span>=<span class="string">&quot;**/AbstractBaseJunit4Test.class&quot;</span> /&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">exclude</span> <span class="attr">name</span>=<span class="string">&quot;**/*$*.class&quot;</span> /&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">include</span> <span class="attr">name</span>=<span class="string">&quot;**/*Test.class&quot;</span> /&gt;</span></span><br><span class="line">                    <span class="tag">&lt;/<span class="name">fileset</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">batchtest</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">junit</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">jacoco:coverage</span>&gt;</span></span><br><span class="line"> </span><br><span class="line">        <span class="comment">&lt;!-- Generate HTML report</span></span><br><span class="line"><span class="comment">            - junit-noframes.html -&gt; Single page HTML-report</span></span><br><span class="line"><span class="comment">            - index.html -&gt; HTML-report using frames (several files, but more comfortable to read)--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">echo</span>&gt;</span>========= 生成Junit报告 =========<span class="tag">&lt;/<span class="name">echo</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">junitreport</span> <span class="attr">todir</span>=<span class="string">&quot;$&#123;test.data.dir&#125;&quot;</span> <span class="attr">description</span>=<span class="string">&quot;单元测试报告&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">fileset</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;test.data.dir&#125;&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">include</span> <span class="attr">name</span>=<span class="string">&quot;TEST-*.xml&quot;</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">fileset</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- 生成一个HTML网页报告 --&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- 自定义报告的格式文件目录，通常从/ANT_HOME/etc/目录直接把junit-frames.xsl和junit-noframes.xsl复制过即可，</span></span><br><span class="line"><span class="comment">            目的是为了自定义报告的标题，默认的报告title为：Unit Test Results</span></span><br><span class="line"><span class="comment">            https://ant.apache.org/manual/Tasks/junitreport.html</span></span><br><span class="line"><span class="comment">            --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">report</span> <span class="attr">format</span>=<span class="string">&quot;frames&quot;</span> <span class="attr">todir</span>=<span class="string">&quot;$&#123;junit.reports.dir&#125;&quot;</span> <span class="attr">styledir</span>=<span class="string">&quot;$&#123;basedir&#125;/junit-report-style&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">param</span> <span class="attr">name</span>=<span class="string">&quot;TITLE&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;junit单元测试报告&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">report</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">junitreport</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">        <span class="comment">&lt;!-- Generate Code Coverage report</span></span><br><span class="line"><span class="comment">            See: http://www.eclemma.org/jacoco/trunk/doc/ant.html --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">echo</span>&gt;</span>========= 生成覆盖率报告 =========<span class="tag">&lt;/<span class="name">echo</span>&gt;</span>    </span><br><span class="line">        <span class="tag">&lt;<span class="name">jacoco:report</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">executiondata</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">file</span> <span class="attr">file</span>=<span class="string">&quot;$&#123;test.data.dir&#125;/jacoco.exec&quot;</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">executiondata</span>&gt;</span></span><br><span class="line"> </span><br><span class="line">            <span class="tag">&lt;<span class="name">structure</span> <span class="attr">name</span>=<span class="string">&quot;单元测试覆盖率报告&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">classfiles</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">fileset</span> <span class="attr">dir</span>=<span class="string">&quot;$&#123;src.classess.dir&#125;&quot;</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">include</span> <span class="attr">name</span>=<span class="string">&quot;**/*.class&quot;</span> /&gt;</span></span><br><span class="line">                        <span class="comment">&lt;!-- 生成的报告中排除test类 --&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">exclude</span> <span class="attr">name</span>=<span class="string">&quot;**/AbstractBaseJunit4Test.class&quot;</span> /&gt;</span></span><br><span class="line">                        <span class="comment">&lt;!-- Exclude inner classes --&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">exclude</span> <span class="attr">name</span>=<span class="string">&quot;**/*$*.class&quot;</span> /&gt;</span></span><br><span class="line">                    <span class="tag">&lt;/<span class="name">fileset</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">classfiles</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">structure</span>&gt;</span></span><br><span class="line"> </span><br><span class="line">            <span class="tag">&lt;<span class="name">html</span> <span class="attr">destdir</span>=<span class="string">&quot;$&#123;coverage.reports.dir&#125;&quot;</span> /&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">jacoco:report</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">target</span>&gt;</span></span><br><span class="line"> </span><br><span class="line">    <span class="tag">&lt;<span class="name">target</span> <span class="attr">name</span>=<span class="string">&quot;main&quot;</span> <span class="attr">depends</span>=<span class="string">&quot;run-test&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>根据实际项目路径修改了一些相对路径，比较重要的有点是最好一个任务中把spring的测试基础类排除了。<code>&lt;exclude name=&quot;**/AbstractBaseJunit4Test.class&quot; /&gt;</code>。<br>在<code>junitreport</code>标签内增加了一个<code>report</code>标签，用于自定义生成的单元测试结果模板。</p>
<p><strong>注意</strong>：最好是打开<code>junit</code>标签中的属性<code>showoutput=&quot;yes&quot;</code>（值为<code>no</code>或者<code>yes</code>），打开这个属性之后所有代码中的<code>Logger</code>日志才会打印到控制台，可以即时看到项目的实时日志方便调试。</p>
<p>打开这个开关之后可以看到有类似如下的日志：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">run-test:</span><br><span class="line">     [<span class="built_in">echo</span>] ========= 运行所有<span class="built_in">test</span>类 =========</span><br><span class="line">[jacoco:coverage] Enhancing junit with coverage</span><br><span class="line">    [junit] objc[21529]: Class JavaLaunchHelper is implemented <span class="keyword">in</span> both /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/bin/java (0x10264b4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x1026c14e0). One of the two will be used. Which one is undefined.</span><br><span class="line">    [junit] 十二月 09, 2018 12:57:26 上午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames</span><br><span class="line">    [junit] 信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]</span><br><span class="line">    [junit] 十二月 09, 2018 12:57:26 上午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners</span><br><span class="line">    [junit] 信息: Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@35e52059, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@62577d6, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@49bd54f7, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6b5f8707]</span><br><span class="line">    [junit] Running com.ubuntuvim.coverage.service.impl.UserServiceImplTest</span><br><span class="line">    [junit] 返回参数： = User [id=123, username=ubuntuvim, age=20]</span><br><span class="line">    [junit] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.61 sec</span><br><span class="line">    </span><br></pre></td></tr></table></figure>

<p>重点是这几行日志：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">…………</span><br><span class="line">[junit] 信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: </span><br><span class="line">…………</span><br><span class="line">[junit] 返回参数： = User [id=123, username=ubuntuvim, age=20]</span><br></pre></td></tr></table></figure>

<p>第一行是容器加载的日志，第二行是业务代码打印的Logger日志。</p>
<h4 id="生成报告"><a href="#生成报告" class="headerlink" title="生成报告"></a>生成报告</h4><p>按照上述内容配置好之后就可以直接运行了。在<code>coverage-report.xml</code>上右键，选中ant运行即可。运行完毕后项目目录下新增<code>reports</code>目录，目录内存放这Junit报告、覆盖率报告。</p>
<p><img src="/image/blog-image/java/junit-coverage/6.png" alt="报告"></p>
<p><code>coverage-report</code>和<code>junit-report</code>分别存放着覆盖率报告和单元测试报告，用浏览器直接打开这两个目录下<code>index.html</code>即可看到报告的详细信息。</p>
<p><img src="/image/blog-image/java/junit-coverage/7.png" alt="junit报告"></p>
<p><img src="/image/blog-image/java/junit-coverage/8.png" alt="覆盖率报告"></p>
<h3 id="项目源码"><a href="#项目源码" class="headerlink" title="项目源码"></a>项目源码</h3><p><a target="_blank" rel="noopener" href="https://github.com/ubuntuvim/coverage/tree/coverage-spring">https://github.com/ubuntuvim/coverage/tree/coverage-spring</a></p>

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/Ember-Demo/%E4%BB%8E%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%8E%B7%E5%8F%96%E6%95%B0%E6%8D%AE%EF%BC%8C%E5%BC%95%E5%85%A5%E7%BB%84%E4%BB%B6/" rel="next" title="从服务器获取数据，引入组件">
          从服务器获取数据，引入组件
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/ember-teach/testing/unitTestingBasics/" rel="prev" title="单元测试基础">
            单元测试基础
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8maven%E6%9E%84%E5%BB%BAspring%E9%A1%B9%E7%9B%AE"><span class="toc-text">使用maven构建spring项目</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B8%85%E7%A9%BA%E5%8E%9F%E6%9C%89%E7%9B%AE%E5%BD%95"><span class="toc-text">清空原有目录</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%BE%E7%BD%AEmaven%E9%A1%B9%E7%9B%AE%E7%BB%93%E6%9E%84"><span class="toc-text">设置maven项目结构</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A2%9E%E5%8A%A0pom-xml"><span class="toc-text">增加pom.xml</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%89%E8%A3%85maven%E4%BE%9D%E8%B5%96"><span class="toc-text">安装maven依赖</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A2%9E%E5%8A%A0%E4%B8%9A%E5%8A%A1%E4%BB%A3%E7%A0%81"><span class="toc-text">增加业务代码</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#spring%E9%85%8D%E7%BD%AE"><span class="toc-text">spring配置</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%AA%8C%E8%AF%81%E9%A1%B9%E7%9B%AE"><span class="toc-text">验证项目</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Spring-Test%E9%85%8D%E7%BD%AE"><span class="toc-text">Spring Test配置</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BABaseJunit4Test%E5%9F%BA%E7%B1%BB"><span class="toc-text">创建BaseJunit4Test基类</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B4%E5%90%88Ant%E8%87%AA%E5%8A%A8%E6%89%AB%E6%8F%8F"><span class="toc-text">整合Ant自动扫描</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E6%8A%A5%E5%91%8A"><span class="toc-text">生成报告</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%A1%B9%E7%9B%AE%E6%BA%90%E7%A0%81"><span class="toc-text">项目源码</span></a></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: 'spring-junit-jacoco代码覆盖测试', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
